<?php
require_once __DIR__ . '/ajax.php';
include_once __DIR__ . '/PHPMailer/PHPMailerAutoload.php';
include_once __DIR__ . '/lang/language.php';

$GLOBALS['trans'] = new Language();

function _trans($key, $params=array()){
    global $trans;
    if(count($params)>0){
        return vsprintf($trans->get($key), $params);
    }
    return $trans->get($key);
}

function _const($key){
    global $trans;
    return $trans->getConst($key);
}

function is_en(){
    $lang = get_query_var('l');
    if($lang==NULL || $lang=='' || $lang=='en'){
        return true;
    }
    return false;
}

function url_lang($url=NULL, $l=NULL){
    if($url==NULL || $url==''){
        $url = get_permalink();
        if($url==''){
            $url = home_url();
        }
    }
    $lang = get_query_var('l');
    if($l == $lang){
        return '#';
    }
    if($l!=NULL){
        $url .= '?l=vi';
    }
    return $url;
}

function url_by_lang($params, $id=true){
    if($id){
        $params = get_permalink($params);
    }
    return add_query_arg($_GET, $params);
}

function get_page_template_by_lang($page){
    $lang = get_query_var('l');
    if($lang==NULL || !in_array($lang, array('en', 'vi'))){
        $lang = 'en';
    }
    $page_content = getTemplate('pages/' . $page, $lang);
    return $page_content;
}

function getTemplate($path, $name, $aVar=array()){
    $mailVars = $aVar;
    ob_start();
    $path = get_template_directory() . '/' . $path;
    require ($path . '/' . $name . '.php');
    $content = ob_get_clean();
    return $content;
}

function assets($item){
    return get_stylesheet_directory_uri(). '/' . $item;
}

function sendMail($aData) {
    // $ggecares_mailer = array(
    //     'receiver_address'      => 'dntruong412@gmail.com',
    //     'receiver_address_name' => 'GGE Cares',
    //     'sender_address'        => 'truongdn.testmail@gmail.com',
    //     'sender_address_name'   => 'GGE Cares',
    //     'mail_host'             => 'smtp.gmail.com',
    //     'mail_host_username'    => 'truongdn.testmail@gmail.com',
    //     'mail_host_password'    => '123456'
    //     );
    // update_option('ggecares_mailer', json_encode($ggecares_mailer));

    $ggecares_mailer = json_decode(get_option('ggecares_mailer'));
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPDebug  = 0;
    $mail->SMTPAuth   = true;
    $mail->CharSet    = 'UTF-8';
    $mail->SMTPSecure = 'ssl';
    $mail->Port       = 465; 
    $mail->Host       = $ggecares_mailer->mail_host;
    $mail->Username   = $ggecares_mailer->mail_host_username;
    $mail->Password   = $ggecares_mailer->mail_host_password;
    $mail->From       = $ggecares_mailer->sender_address;
    $mail->FromName   = $ggecares_mailer->sender_address_name;
    $mail->addAddress($ggecares_mailer->receiver_address, $ggecares_mailer->receiver_address_name);
    $mail->Subject    = $aData['subject'];
    $mail->Body       = $aData['body'];
    if (isset($aData['attach'])) {
        $fileName = __dir__ . '/upload/' . $aData['attach'];
        $mail->addAttachment($fileName);
    }
    $mail->WordWrap = 70;
    $mail->IsHTML(true);


    if(!$mail->Send()) {
        // echo "Message could not be sent. <p>";
        // echo "Mailer Error: " . $mail->ErrorInfo;
        return false;
    }
    // echo "Message has been sent";
    return true;
}

function contactUs($aData) {
    foreach ($aData as $key => &$value) {
        $value = sanitize_text_field(trim($value));
    }
    $mailVars = array(
        'name' => $aData['name'],
        'telephone' => $aData['telephone'],
        'email' => $aData['email'],
        'message' => $aData['message']);
    $mailData['subject'] = 'GGECares - Contact Us';
    $mailData['body'] = getTemplate('mail-templates', 'contact-us.html', $mailVars);
    $response = array();
    if (sendMail($mailData)) {
        $response['code'] = 1;
        $response['message'] = 'Send message success!';
    } else {
        $response['code'] = 0;
        $response['message'] = 'Send message failed!';
    }
    echo json_encode($response);
    die();
}

function registerTuition($aData) {
    foreach ($aData as $key => &$value) {
        $value = sanitize_text_field(trim($value));
    }
    $mailVars = array(
        'name' => $aData['name'],
        'telephone' => $aData['telephone'],
        'email' => $aData['email'],
        'duration' => $aData['duration'],
        'datetime' => $aData['date-time'],
        'venue' => $aData['venue'],
        'first-language' => $aData['first-language'],
        'other-languages' => $aData['other-languages'],
        'other-educationalqualify' => $aData['other-educationalqualify'],
        'current-employment' => $aData['current-employment'],
        'area-focus' => $aData['area-focus'],
        'other-info' => $aData['other-info']);
    $mailData['subject'] = 'GGECares - Private Tuition';
    $mailData['body'] = getTemplate('mail-templates', 'private-tuition.html', $mailVars);
    $response = array();
    if (sendMail($mailData)) {
        $response['code'] = 1;
        $response['message'] = 'Send message success!';
    } else {
        $response['code'] = 0;
        $response['message'] = 'Send message failed!';
    }
    echo json_encode($response);
    die();
}

function registerEmployment($aData) {
    foreach ($aData as $key => &$value) {
        $value = sanitize_text_field(trim($value));
    }
    $mailVars = array(
        'name' => $aData['name'],
        'telephone' => $aData['telephone'],
        'email' => $aData['email'],
        'skype' => $aData['skype'],
        'introduce' => $aData['introduce'],
        'cv' => $aData['cv']);
    $mailData['subject'] = 'GGECares - Employment';
    $mailData['body'] = getTemplate('mail-templates', 'employment.html', $mailVars);
    if (isset($aData['cv']) && $aData['cv'] != '') {
        $mailData['attach'] = $aData['cv'];
    }
    $response = array();
    if (sendMail($mailData)) {
        $response['code'] = 1;
        $response['message'] = 'Send message success!';
    } else {
        $response['code'] = 0;
        $response['message'] = 'Send message failed!';
    }
    echo json_encode($response);
    die();
}

function newEnquiryForm($aData) {
    foreach ($aData as $key => &$value) {
        $value = sanitize_text_field(trim($value));
    }
    $mailVars = array(
        'name' => $aData['fullname'],
        'telephone' => $aData['telephone'],
        'email' => $aData['email'],
        'living' => $aData['livein'],
        'coursetype' => $aData['coursetype'],
        'course' => $aData['course'],
        'enquiry' => $aData['enquiry']);
    $mailData['subject'] = 'GGECares - Quick Enquiry Form';
    $mailData['body'] = getTemplate('mail-templates', 'quick-enquiry-form.html', $mailVars);
    $response = array();
    if (sendMail($mailData)) {
        $response['code'] = 1;
        $response['message'] = 'Register success!';
    } else {
        $response['code'] = 0;
        $response['message'] = 'Register failed!';
    }
    echo json_encode($response);
    die();
}

function uploadCV($uploadFile) {
    if($uploadFile==NULL || $uploadFile["error"] != 0) {
        $aResult = array('code'=>0);
    } else {
        $aAcceptFileType = array('application/pdf', 'application/msword');
        if(!in_array($uploadFile['type'], $aAcceptFileType)){
            $aResult = array('code'=>0);
        } else {
            $now = new DateTime('now', new DateTimeZone('Asia/Ho_Chi_Minh'));
            $now = $now->format(YmdHis);
            $fileName = $now . '-' . $uploadFile['name'];
            move_uploaded_file($uploadFile["tmp_name"], __dir__ . "/upload/" . $fileName);
            $aResult = array('code'=>1, 'name' => $fileName);
        }
    }
    echo json_encode($aResult);
    die();
}
?>